var api = require("../../api/api.js");
var utils = require("../../utils/utils.js");
var event = require("../../event/event.js");
var storageManage = require("../../manage/storageManage.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: '',
    goodsList: [],
    storeType: storageManage.getGlobalConfig().store_type,
    store_id: '',
    store_index: 0,
    mark: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: options.uid,
      store_id: this.data.storeType[this.data.store_index].id
    })
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getData: function () {
    api.default.api('check_detail', {
      user_id: this.data.uid
    }, this.successCallback, this.errorCallback);
  },
  successCallback: function (res) {
    var data = res.data;
    var goodsList=[];
    var that=this;
    if (data.commodity_prices.length>=1){
      goodsList = data.commodity_prices;
    }else{
      goodsList = storageManage.getGlobalConfig().commodity_info;
      
    };
    for (let i = 0; i < that.data.storeType.length;i++){ //id对比获取下标 
      console.log(that.data.storeType[i].id)
      if (that.data.storeType[i].id == data.store_type){
        that.setData({
          store_index:i
        })
        }
    }
    this.setData({
      "goodsList": goodsList,
      "address": data.address,
      "icon": data.icon,
      "phone": data.phone,
      "store_count": data.store_count,
      "store_name": data.store_name,
      "user_name": data.name,
      "user_state": data.state,
      "store_id": data.store_type,
      "mark": data.detail
    })
  },
  errorCallback: function () {

  },
  editPrice: function (e) {
    var index = e.currentTarget.dataset.idx;
    var goodsListItme = "goodsList[" + index + "].isChecked";
    var goodsList = this.data.goodsList;
    for (let i = 0; i < this.data.goodsList.length; i++) {
      goodsList[i].isChecked = false;
    }
    this.setData({
      goodsList: goodsList,
      [goodsListItme]: true
    })
    this.setData({
    })
  },
  changePrice: function (e) {

    console.log("失去焦点")
    var index = e.currentTarget.dataset.idx;
    var val = e.detail.value;
    var priceItme = "goodsList[" + index + "].price";
    var goodsList = this.data.goodsList;
    for (let i = 0; i < this.data.goodsList.length; i++) {
      goodsList[i].isChecked = false;
    }
    this.setData({
      [priceItme]: val
    })

  },
  markChange: function (e) {
    this.setData({
      mark: e.detail.value
    })
  },
  submit: utils.throttle(function (e) {
    var store_status = e.currentTarget.dataset.status;
    this.data.store_status = store_status;
    var goodsList = this.data.goodsList;
    var commodity_prices = [];
    var status = false;
    for (let i = 0; i < goodsList.length; i++) {
      if (!utils.isNum(goodsList[i].price) || utils.isEmpty(goodsList[i].price)) {
        status = true
      }
      commodity_prices.push({
        "commodity_id": goodsList[i].commodity_id,
        "price": parseFloat(goodsList[i].price)
      })
    }
    if (status) {
      wx.showToast({
        title: '价格输入不正确',
        icon: 'none'
      })
      return
    }

    var payload = {
      "store_status": store_status,
      "user_id": this.data.uid,
      "store_type": this.data.store_id.toString(),
      "detail": this.data.mark,
      "commodity_prices": commodity_prices
    }
    api.default.api('check_store', payload, this.checkSuccessCallback, this.checkErrorCallback);

  }),
  checkSuccessCallback: function (res) {
    var store_status;
    if (this.data.store_status == 3) {//通过
      store_status = 2 //时间通知要跳转为已通过
    } else {
      store_status = 0
    }
    event.notify("check_store", {
      store_status: store_status
    });
    wx.showToast({
      title: '提交成功',
    })
    setTimeout(function(){
      wx.navigateBack()
    },1000)
  },
  checkErrorCallback: function (e) {

  },
  storeTypeChange: function (e) {
    var index = e.detail.value;
    this.setData({
      store_index: index,
      store_id: this.data.storeType[index].id
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})